package com.nursinghome.admin.service.impl;

import com.nursinghome.admin.mapper.ElderlyMapper;
import com.nursinghome.admin.mapper.RoomMapper;
import com.nursinghome.admin.service.ElderlyService;
import com.nursinghome.bean.Elderly;
import com.nursinghome.exception.NurisingHomeException;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.List;

@Service
public class ElderlyServiceImpl implements ElderlyService {
    @Autowired
    private ElderlyMapper elderlyMapper;
    @Autowired
    private RoomMapper roomMapper;
    @Override
    public List<Elderly> selectAll(String ename, String eusername) {
        return elderlyMapper.selectAll(ename,eusername);
    }

    @Transactional(rollbackFor = Exception.class)
    @Override
    public Boolean addElderly(Elderly elderly) throws NurisingHomeException {
        //判断所有新增的老人是否eusername重复
        Elderly byEusername = elderlyMapper.selectByEusername(elderly.getEusername());
        if (byEusername != null){
            throw new NurisingHomeException("该用户名已经被占用请重新输入");
        }
        elderlyMapper.addElderly(elderly);
        roomMapper.updateIn(elderly.getRid());
        return true;
    }

    @Transactional(rollbackFor = Exception.class)
    @Override
    public Boolean delByEid(Long eid) throws NurisingHomeException {
        //判断老人是否存在
        Elderly delByEid = elderlyMapper.selectByEid(eid);
        if (delByEid == null){
            throw new NurisingHomeException("老人不存在无法删除！");
        }
        elderlyMapper.delByEid(eid);
        elderlyMapper.deleteByEidMedical(eid);
        roomMapper.updateOut(delByEid.getRid());
        Elderly elderly = elderlyMapper.selectByEid(eid);
        if (elderly != null){
            roomMapper.updateOut(elderly.getRid());
        }
        return true;
    }

    @Transactional(rollbackFor = Exception.class)
    @Override
    public Boolean update(Elderly elderly) throws NurisingHomeException {
        //判断老人是否存在
        Elderly ByEid = elderlyMapper.selectByEid(elderly.getEid());
        if (ByEid == null){
            throw new NurisingHomeException("老人不存在无法修改！");
        }
        //判断修改之后的用户名是否于其他用户名重复
        Elderly byEusername = elderlyMapper.selectByEusername(elderly.getEusername());
        if (byEusername != null &&
                !byEusername.getEusername().equals(ByEid.getEusername())){
            throw new NurisingHomeException("修改后的老人用户名重复，无法修改");
        }
        roomMapper.updateIn(elderly.getRid());

        return elderlyMapper.update(elderly) == 1;
    }

    @Override
    public Elderly selectByEid(Long eid) {
        return elderlyMapper.selectByEid(eid);
    }

    @Override
    public Elderly selectByEusername(String eusername) {
        return elderlyMapper.selectByEusername(eusername);
    }

    @Override
    @Transactional(rollbackFor = Exception.class)
    public Boolean assignCaregivers(Long eid, Integer[] mids) {
        elderlyMapper.deleteByEidMedical(eid);
        if (mids.length > 0){
            elderlyMapper.insertElderlyMedicel(eid, mids);
        }
        return true;
    }

    //根据老人id查询护工id
    @Override
    public Integer[] selectByEidAndEM(Long eid) {
        return elderlyMapper.selectByEidAndEM(eid);
    }
}
